{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:45.686698Z",
     "end_time": "2024-06-18T17:29:45.696907Z"
    }
   },
   "outputs": [],
   "source": [
    "def get_sample(df, sampling=\"simple_random\", k=1, stratified_col=None):\n",
    "    \"\"\"\n",
    "    对输入的 dataframe 进行抽样的函数\n",
    "\n",
    "    参数:\n",
    "        - df: 输入的数据框 pandas.dataframe 对象\n",
    "\n",
    "        - sampling:抽样方法 str\n",
    "            可选值有 [\"simple_random\", \"stratified\", \"systematic\"]\n",
    "            按顺序分别为: 简单随机抽样、分层抽样、系统抽样\n",
    "\n",
    "        - k: 抽样个数或抽样比例 int or float\n",
    "            (int, 则必须大于0; float, 则必须在区间(0,1)中)\n",
    "            如果 0 < k < 1 , 则 k 表示抽样对于总体的比例\n",
    "            如果 k >= 1 , 则 k 表示抽样的个数；当为分层抽样时，代表每层的样本量\n",
    "\n",
    "        - stratified_col: 需要分层的列名的列表 list\n",
    "            只有在分层抽样时才生效\n",
    "\n",
    "    返回值:\n",
    "        pandas.dataframe 对象, 抽样结果\n",
    "    \"\"\"\n",
    "    import random\n",
    "    import pandas as pd\n",
    "    from functools import reduce\n",
    "    import numpy as np\n",
    "    import math\n",
    "\n",
    "    len_df = len(df)\n",
    "    if k <= 0:\n",
    "        raise AssertionError(\"k不能为负数\")\n",
    "    elif k >= 1:\n",
    "        assert isinstance(k, int), \"选择抽样个数时, k必须为正整数\"\n",
    "        sample_by_n = True\n",
    "        if sampling == \"stratified\":\n",
    "            alln = k * df.groupby(by=stratified_col)[stratified_col[0]].count().count()  # 有问题的\n",
    "            #alln=k*df[stratified_col].value_counts().count()\n",
    "            if alln >= len_df:\n",
    "                raise AssertionError(\"请确认k乘以层数不能超过总样本量\")\n",
    "    else:\n",
    "        sample_by_n = False\n",
    "        if sampling in (\"simple_random\", \"systematic\"):\n",
    "            k = math.ceil(len_df * k)\n",
    "\n",
    "    #print(k)\n",
    "\n",
    "    if sampling == \"simple_random\":\n",
    "        print(\"使用简单随机抽样\")\n",
    "        idx = random.sample(range(len_df), k)\n",
    "        res_df = df.iloc[idx, :].copy()\n",
    "        return res_df\n",
    "\n",
    "    elif sampling == \"systematic\":\n",
    "        print(\"使用系统抽样\")\n",
    "        step = len_df // k + 1  #step=len_df//k-1\n",
    "        start = 0  #start=0\n",
    "        idx = range(len_df)[start::step]  #idx=range(len_df+1)[start::step]\n",
    "        res_df = df.iloc[idx, :].copy()\n",
    "        #print(\"k=%d,step=%d,idx=%d\"%(k,step,len(idx)))\n",
    "        return res_df\n",
    "\n",
    "    elif sampling == \"stratified\":\n",
    "        assert stratified_col is not None, \"请传入包含需要分层的列名的列表\"\n",
    "        assert all(np.in1d(stratified_col, df.columns)), \"请检查输入的列名\"\n",
    "\n",
    "        grouped = df.groupby(by=stratified_col)[stratified_col[0]].count()\n",
    "        if sample_by_n == True:\n",
    "            group_k = grouped.map(lambda x: k)\n",
    "        else:\n",
    "            group_k = grouped.map(lambda x: math.ceil(x * k))\n",
    "\n",
    "        res_df = pd.DataFrame(columns=df.columns)\n",
    "        for df_idx in group_k.index:\n",
    "            df1 = df\n",
    "            if len(stratified_col) == 1:\n",
    "                df1 = df1[df1[stratified_col[0]] == df_idx]\n",
    "            else:\n",
    "                for i in range(len(df_idx)):\n",
    "                    df1 = df1[df1[stratified_col[i]] == df_idx[i]]\n",
    "            idx = random.sample(range(len(df1)), group_k[df_idx])\n",
    "            group_df = df1.iloc[idx, :].copy()\n",
    "            res_df = pd.concat([res_df, group_df])\n",
    "        return res_df\n",
    "\n",
    "    else:\n",
    "        raise AssertionError(\"sampling is illegal\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "data": {
      "text/plain": "   client_id sex  birth_date  district_id\n0          1   女  1970-12-13           18\n1          2   男  1945-02-04            1\n2          3   女  1940-10-09            1\n3          4   男  1956-12-01            5\n4          5   女  1960-07-03            5",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>女</td>\n      <td>1970-12-13</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>2</td>\n      <td>男</td>\n      <td>1945-02-04</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>3</td>\n      <td>女</td>\n      <td>1940-10-09</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4</td>\n      <td>男</td>\n      <td>1956-12-01</td>\n      <td>5</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5</td>\n      <td>女</td>\n      <td>1960-07-03</td>\n      <td>5</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "clients = pd.read_csv('../data/bank/clients.csv', encoding='gbk')\n",
    "clients.head()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:45.699918Z",
     "end_time": "2024-06-18T17:29:46.217125Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用简单随机抽样\n"
     ]
    },
    {
     "data": {
      "text/plain": "      client_id sex  birth_date  district_id\n4537       4797   女  1940-09-03           59\n1753       1861   男  1957-07-15           29\n3272       3457   女  1923-06-14           75\n2216       2342   男  1948-12-18           70\n1355       1431   女  1930-11-08           55\n191         202   女  1976-11-21           29\n2382       2511   女  1975-03-27           35\n3800       4014   男  1980-09-06           17\n2544       2686   女  1951-03-24           19\n1850       1961   女  1920-06-04           31\n3831       4048   男  1923-11-24            4\n5027       9641   男  1945-01-26           64\n3068       3241   男  1934-09-14           26\n4940       8076   女  1950-07-27           16\n1200       1262   男  1952-04-03           35\n4923       7797   男  1970-07-30           50\n3769       3981   男  1979-06-16           19\n4653       5203   女  1945-09-08           61\n3923       4147   男  1972-12-23           64\n5136      11063   女  1955-11-04           64\n3567       3768   女  1944-03-04           14\n3108       3282   男  1981-09-02           29",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>4537</th>\n      <td>4797</td>\n      <td>女</td>\n      <td>1940-09-03</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>1753</th>\n      <td>1861</td>\n      <td>男</td>\n      <td>1957-07-15</td>\n      <td>29</td>\n    </tr>\n    <tr>\n      <th>3272</th>\n      <td>3457</td>\n      <td>女</td>\n      <td>1923-06-14</td>\n      <td>75</td>\n    </tr>\n    <tr>\n      <th>2216</th>\n      <td>2342</td>\n      <td>男</td>\n      <td>1948-12-18</td>\n      <td>70</td>\n    </tr>\n    <tr>\n      <th>1355</th>\n      <td>1431</td>\n      <td>女</td>\n      <td>1930-11-08</td>\n      <td>55</td>\n    </tr>\n    <tr>\n      <th>191</th>\n      <td>202</td>\n      <td>女</td>\n      <td>1976-11-21</td>\n      <td>29</td>\n    </tr>\n    <tr>\n      <th>2382</th>\n      <td>2511</td>\n      <td>女</td>\n      <td>1975-03-27</td>\n      <td>35</td>\n    </tr>\n    <tr>\n      <th>3800</th>\n      <td>4014</td>\n      <td>男</td>\n      <td>1980-09-06</td>\n      <td>17</td>\n    </tr>\n    <tr>\n      <th>2544</th>\n      <td>2686</td>\n      <td>女</td>\n      <td>1951-03-24</td>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>1850</th>\n      <td>1961</td>\n      <td>女</td>\n      <td>1920-06-04</td>\n      <td>31</td>\n    </tr>\n    <tr>\n      <th>3831</th>\n      <td>4048</td>\n      <td>男</td>\n      <td>1923-11-24</td>\n      <td>4</td>\n    </tr>\n    <tr>\n      <th>5027</th>\n      <td>9641</td>\n      <td>男</td>\n      <td>1945-01-26</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th>3068</th>\n      <td>3241</td>\n      <td>男</td>\n      <td>1934-09-14</td>\n      <td>26</td>\n    </tr>\n    <tr>\n      <th>4940</th>\n      <td>8076</td>\n      <td>女</td>\n      <td>1950-07-27</td>\n      <td>16</td>\n    </tr>\n    <tr>\n      <th>1200</th>\n      <td>1262</td>\n      <td>男</td>\n      <td>1952-04-03</td>\n      <td>35</td>\n    </tr>\n    <tr>\n      <th>4923</th>\n      <td>7797</td>\n      <td>男</td>\n      <td>1970-07-30</td>\n      <td>50</td>\n    </tr>\n    <tr>\n      <th>3769</th>\n      <td>3981</td>\n      <td>男</td>\n      <td>1979-06-16</td>\n      <td>19</td>\n    </tr>\n    <tr>\n      <th>4653</th>\n      <td>5203</td>\n      <td>女</td>\n      <td>1945-09-08</td>\n      <td>61</td>\n    </tr>\n    <tr>\n      <th>3923</th>\n      <td>4147</td>\n      <td>男</td>\n      <td>1972-12-23</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th>5136</th>\n      <td>11063</td>\n      <td>女</td>\n      <td>1955-11-04</td>\n      <td>64</td>\n    </tr>\n    <tr>\n      <th>3567</th>\n      <td>3768</td>\n      <td>女</td>\n      <td>1944-03-04</td>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>3108</th>\n      <td>3282</td>\n      <td>男</td>\n      <td>1981-09-02</td>\n      <td>29</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 1 简单随机抽样\n",
    "# 简单随机抽样-按数量取\n",
    "srn = get_sample(clients, sampling=\"simple_random\", k=22, stratified_col=None)\n",
    "srn"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.217125Z",
     "end_time": "2024-06-18T17:29:46.230038Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用简单随机抽样\n"
     ]
    },
    {
     "data": {
      "text/plain": "      client_id sex  birth_date  district_id\n4000       4231   女  1959-08-08            1\n4276       4523   男  1952-03-09           68\n3856       4074   女  1958-06-08           24\n3330       3515   女  1975-01-02           61\n0             1   女  1970-12-13           18\n...         ...  ..         ...          ...\n2601       2749   女  1921-03-04           14\n5152      11208   女  1957-03-05           10\n3395       3583   女  1942-09-09           15\n3436       3628   女  1927-02-26           11\n413         434   女  1943-01-30           70\n\n[537 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>4000</th>\n      <td>4231</td>\n      <td>女</td>\n      <td>1959-08-08</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4276</th>\n      <td>4523</td>\n      <td>男</td>\n      <td>1952-03-09</td>\n      <td>68</td>\n    </tr>\n    <tr>\n      <th>3856</th>\n      <td>4074</td>\n      <td>女</td>\n      <td>1958-06-08</td>\n      <td>24</td>\n    </tr>\n    <tr>\n      <th>3330</th>\n      <td>3515</td>\n      <td>女</td>\n      <td>1975-01-02</td>\n      <td>61</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>女</td>\n      <td>1970-12-13</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2601</th>\n      <td>2749</td>\n      <td>女</td>\n      <td>1921-03-04</td>\n      <td>14</td>\n    </tr>\n    <tr>\n      <th>5152</th>\n      <td>11208</td>\n      <td>女</td>\n      <td>1957-03-05</td>\n      <td>10</td>\n    </tr>\n    <tr>\n      <th>3395</th>\n      <td>3583</td>\n      <td>女</td>\n      <td>1942-09-09</td>\n      <td>15</td>\n    </tr>\n    <tr>\n      <th>3436</th>\n      <td>3628</td>\n      <td>女</td>\n      <td>1927-02-26</td>\n      <td>11</td>\n    </tr>\n    <tr>\n      <th>413</th>\n      <td>434</td>\n      <td>女</td>\n      <td>1943-01-30</td>\n      <td>70</td>\n    </tr>\n  </tbody>\n</table>\n<p>537 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 简单随机抽样-按百分比取\n",
    "srp = get_sample(clients, sampling=\"simple_random\", k=0.1, stratified_col=None)\n",
    "srp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.233025Z",
     "end_time": "2024-06-18T17:29:46.289898Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "     client_id sex  birth_date district_id\n1845      1956   女  1925-07-09           1\n969       1023   男  1938-12-28           1\n1010      1065   男  1957-11-26           2\n5236     12144   男  1980-11-23           2\n4881      7127   男  1957-09-29           3\n...        ...  ..         ...         ...\n2316      2443   男  1949-05-23          75\n3812      4026   女  1945-09-24          76\n4502      4762   女  1971-02-13          76\n5359     13915   男  1974-11-23          77\n3630      3836   男  1948-11-21          77\n\n[154 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>1845</th>\n      <td>1956</td>\n      <td>女</td>\n      <td>1925-07-09</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>969</th>\n      <td>1023</td>\n      <td>男</td>\n      <td>1938-12-28</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1010</th>\n      <td>1065</td>\n      <td>男</td>\n      <td>1957-11-26</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>5236</th>\n      <td>12144</td>\n      <td>男</td>\n      <td>1980-11-23</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>4881</th>\n      <td>7127</td>\n      <td>男</td>\n      <td>1957-09-29</td>\n      <td>3</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>2316</th>\n      <td>2443</td>\n      <td>男</td>\n      <td>1949-05-23</td>\n      <td>75</td>\n    </tr>\n    <tr>\n      <th>3812</th>\n      <td>4026</td>\n      <td>女</td>\n      <td>1945-09-24</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>4502</th>\n      <td>4762</td>\n      <td>女</td>\n      <td>1971-02-13</td>\n      <td>76</td>\n    </tr>\n    <tr>\n      <th>5359</th>\n      <td>13915</td>\n      <td>男</td>\n      <td>1974-11-23</td>\n      <td>77</td>\n    </tr>\n    <tr>\n      <th>3630</th>\n      <td>3836</td>\n      <td>男</td>\n      <td>1948-11-21</td>\n      <td>77</td>\n    </tr>\n  </tbody>\n</table>\n<p>154 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2 分层抽样\n",
    "# 分层抽样-按每层数量取\n",
    "strn = get_sample(clients, sampling=\"stratified\", k=2, stratified_col=['district_id'])\n",
    "strn"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.243890Z",
     "end_time": "2024-06-18T17:29:46.419248Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [
    {
     "data": {
      "text/plain": "     client_id sex  birth_date district_id\n4236      4481   男  1975-06-16           1\n4374      4627   男  1942-03-24           1\n1901      2015   女  1968-11-15           1\n3926      4150   女  1977-08-10           1\n1884      1997   女  1977-11-09           1\n...        ...  ..         ...         ...\n4639      5143   男  1973-03-09          77\n1817      1927   男  1952-04-26          77\n4876      7043   男  1940-06-30          77\n2665      2820   男  1966-12-17          77\n58          62   男  1932-12-08          77\n\n[573 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>4236</th>\n      <td>4481</td>\n      <td>男</td>\n      <td>1975-06-16</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4374</th>\n      <td>4627</td>\n      <td>男</td>\n      <td>1942-03-24</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1901</th>\n      <td>2015</td>\n      <td>女</td>\n      <td>1968-11-15</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3926</th>\n      <td>4150</td>\n      <td>女</td>\n      <td>1977-08-10</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1884</th>\n      <td>1997</td>\n      <td>女</td>\n      <td>1977-11-09</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>4639</th>\n      <td>5143</td>\n      <td>男</td>\n      <td>1973-03-09</td>\n      <td>77</td>\n    </tr>\n    <tr>\n      <th>1817</th>\n      <td>1927</td>\n      <td>男</td>\n      <td>1952-04-26</td>\n      <td>77</td>\n    </tr>\n    <tr>\n      <th>4876</th>\n      <td>7043</td>\n      <td>男</td>\n      <td>1940-06-30</td>\n      <td>77</td>\n    </tr>\n    <tr>\n      <th>2665</th>\n      <td>2820</td>\n      <td>男</td>\n      <td>1966-12-17</td>\n      <td>77</td>\n    </tr>\n    <tr>\n      <th>58</th>\n      <td>62</td>\n      <td>男</td>\n      <td>1932-12-08</td>\n      <td>77</td>\n    </tr>\n  </tbody>\n</table>\n<p>573 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 分层抽样-按每层百分比取\n",
    "strp = get_sample(clients, sampling=\"stratified\", k=0.1, stratified_col=['district_id'])\n",
    "strp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.302862Z",
     "end_time": "2024-06-18T17:29:46.443252Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用系统抽样\n"
     ]
    },
    {
     "data": {
      "text/plain": "      client_id sex  birth_date  district_id\n0             1   女  1970-12-13           18\n1343       1419   女  1918-01-25           74\n2686       2841   女  1973-04-10           18\n4029       4260   男  1967-06-01           12",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>女</td>\n      <td>1970-12-13</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>1343</th>\n      <td>1419</td>\n      <td>女</td>\n      <td>1918-01-25</td>\n      <td>74</td>\n    </tr>\n    <tr>\n      <th>2686</th>\n      <td>2841</td>\n      <td>女</td>\n      <td>1973-04-10</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>4029</th>\n      <td>4260</td>\n      <td>男</td>\n      <td>1967-06-01</td>\n      <td>12</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 3 系统抽样\n",
    "# 系统抽样-按数量取\n",
    "sysn = get_sample(clients, sampling=\"systematic\", k=4, stratified_col=None)\n",
    "sysn"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.366307Z",
     "end_time": "2024-06-18T17:29:46.443252Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "使用系统抽样\n"
     ]
    },
    {
     "data": {
      "text/plain": "      client_id sex  birth_date  district_id\n0             1   女  1970-12-13           18\n10           11   女  1950-08-22           57\n20           21   女  1952-10-29           12\n30           31   男  1962-02-09           68\n40           43   男  1920-10-28           75\n...         ...  ..         ...          ...\n5320      13291   女  1940-11-13           16\n5330      13451   女  1955-11-18           44\n5340      13590   男  1977-08-03           59\n5350      13694   女  1976-01-20            1\n5360      13921   女  1970-12-20           62\n\n[537 rows x 4 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>client_id</th>\n      <th>sex</th>\n      <th>birth_date</th>\n      <th>district_id</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>1</td>\n      <td>女</td>\n      <td>1970-12-13</td>\n      <td>18</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>11</td>\n      <td>女</td>\n      <td>1950-08-22</td>\n      <td>57</td>\n    </tr>\n    <tr>\n      <th>20</th>\n      <td>21</td>\n      <td>女</td>\n      <td>1952-10-29</td>\n      <td>12</td>\n    </tr>\n    <tr>\n      <th>30</th>\n      <td>31</td>\n      <td>男</td>\n      <td>1962-02-09</td>\n      <td>68</td>\n    </tr>\n    <tr>\n      <th>40</th>\n      <td>43</td>\n      <td>男</td>\n      <td>1920-10-28</td>\n      <td>75</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>5320</th>\n      <td>13291</td>\n      <td>女</td>\n      <td>1940-11-13</td>\n      <td>16</td>\n    </tr>\n    <tr>\n      <th>5330</th>\n      <td>13451</td>\n      <td>女</td>\n      <td>1955-11-18</td>\n      <td>44</td>\n    </tr>\n    <tr>\n      <th>5340</th>\n      <td>13590</td>\n      <td>男</td>\n      <td>1977-08-03</td>\n      <td>59</td>\n    </tr>\n    <tr>\n      <th>5350</th>\n      <td>13694</td>\n      <td>女</td>\n      <td>1976-01-20</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5360</th>\n      <td>13921</td>\n      <td>女</td>\n      <td>1970-12-20</td>\n      <td>62</td>\n    </tr>\n  </tbody>\n</table>\n<p>537 rows × 4 columns</p>\n</div>"
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 系统抽样-按百分比取\n",
    "sysp = get_sample(clients, sampling=\"systematic\", k=0.1, stratified_col=None)\n",
    "sysp"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.379649Z",
     "end_time": "2024-06-18T17:29:46.444256Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-06-18T17:29:46.390726Z",
     "end_time": "2024-06-18T17:29:46.478248Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
